<template>
  <a-spin :spinning="confirmLoading">
    <a-table
      ref="table"
      size="middle"
      :scroll="{x:true}"
      bordered
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      class="j-table-force-nowrap">

      <template slot="htmlSlot" slot-scope="text">
        <div v-html="text"></div>
      </template>

    </a-table>
  </a-spin>
</template>

<script>

    import { httpAction, getAction } from '@/api/manage'
    import pick from 'lodash.pick'
    import { validateDuplicateValue } from '@/utils/util'
    import JFormContainer from '@/components/jeecg/JFormContainer'

    export default {
        name: 'LhygFinancemanageForm',
        components: {
            JFormContainer,
        },
        props: {
            //流程表单data
            formData: {
                type: Object,
                default: ()=>{},
                required: false
            },
            //表单模式：true流程表单 false普通表单
            formBpm: {
                type: Boolean,
                default: false,
                required: false
            },
            //表单禁用
            disabled: {
                type: Boolean,
                default: false,
                required: false
            }
        },
        data () {
            return {
                form: this.$form.createForm(this),
                model: {},
                confirmLoading: false,
                columns: [
                    {
                        title:'企业名称',
                        align:"center",
                        dataIndex: 'gsmc'
                    },
                    {
                        title:'项目名称',
                        align:"center",
                        dataIndex: 'xmmc'
                    },
                    {
                        title:'项目单号',
                        align:"center",
                        dataIndex: 'itembh'
                    },
                    {
                        title:'交易金额',
                        align:"center",
                        dataIndex: 'fkje'
                    },
                    {
                        title:'余额',
                        align:"center",
                        dataIndex: 'balance'
                    },
                    {
                        title:'类型',
                        align:"center",
                        dataIndex: 'type',
                        customRender: function (t) {
                            if(t == 1){
                                return "提现"
                            } else {
                                return "佣金"
                            }
                        }
                    }
                ],
                url: {
                    list: "/project/lhygWxMoneyDetail/list",
                    add: "/project/lhygWxMoneyDetail/add",
                    edit: "/project/lhygWxMoneyDetail/edit",
                    queryById: "/project/lhygWxMoneyDetail/queryById"
                },
                /* 数据源 */
                dataSource:[],
                /* 分页参数 */
                ipagination:{
                    current: 1,
                    pageSize: 10,
                    pageSizeOptions: ['10', '20', '30'],
                    showTotal: (total, range) => {
                        return range[0] + "-" + range[1] + " 共" + total + "条"
                    },
                    showQuickJumper: true,
                    showSizeChanger: true,
                    total: 0
                },
                selectedRowKeys: [],
                /* table选中records*/
                selectionRows: [],
            }
        },
        computed: {
            formDisabled(){
                if(this.formBpm===true){
                    if(this.formData.disabled===false){
                        return false
                    }
                    return true
                }
                return this.disabled
            },
            showFlowSubmitButton(){
                if(this.formBpm===true){
                    if(this.formData.disabled===false){
                        return true
                    }
                }
                return false
            }
        },
        created () {
            //如果是流程中表单，则需要加载流程表单data
            this.showFlowData();
        },
        methods: {
            add () {
                this.edit({});
            },
            edit (record) {
                getAction(this.url.list, {userid: record.id}).then((res) => {
                    if (res.success) {
                        this.dataSource = res.result.records;
                        this.ipagination.total = res.result.total;
                    }
                    if(res.code===510){
                        this.$message.warning(res.message)
                    }
                    this.loading = false;
                })
            },
            //渲染流程表单数据
            showFlowData(){
                if(this.formBpm === true){
                    let params = {id:this.formData.dataId};
                    getAction(this.url.queryById,params).then((res)=>{
                        if(res.success){
                            this.edit (res.result);
                        }
                    });
                }
            },
            submitForm () {
                const that = this;
                // 触发表单验证
                this.form.validateFields((err, values) => {
                    if (!err) {
                        that.confirmLoading = true;
                        let httpurl = '';
                        let method = '';
                        if(!this.model.id){
                            httpurl+=this.url.add;
                            method = 'post';
                        }else{
                            httpurl+=this.url.edit;
                            method = 'put';
                        }
                        let formData = Object.assign(this.model, values);
                        console.log("表单提交数据",formData)
                        httpAction(httpurl,formData,method).then((res)=>{
                            if(res.success){
                                that.$message.success(res.message);
                                that.$emit('ok');
                            }else{
                                that.$message.warning(res.message);
                            }
                        }).finally(() => {
                            that.confirmLoading = false;
                        })
                    }

                })
            },
            popupCallback(row){
                this.form.setFieldsValue(pick(row,'userid','xmmc','itembh','gsmc','fkje','balance','bankNum','type','serialCode','bankName'))
            },
            onSelectChange(selectedRowKeys, selectionRows) {
                this.selectedRowKeys = selectedRowKeys;
                this.selectionRows = selectionRows;
            },
            onClearSelected() {
                this.selectedRowKeys = [];
                this.selectionRows = [];
            },
        }
    }
</script>